package cn.ronghuanet.user.controller;

import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.StrUtils;
import cn.ronghuanet.base.util.VerifyCodeUtils;
import cn.ronghuanet.user.dto.RegisterSendSmsCodeDTO;
import cn.ronghuanet.user.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 前后端不分离模式下获取图片验证码
     */
/*    @RequestMapping("/image")
    public void getImageCode(HttpSession session, HttpServletResponse response)throws Exception{
        // 1 生成6位数的随机字符串
        String randomStr = StrUtils.getComplexRandomString(6);
        System.out.println(randomStr);

        // 2 将生成好的随机字符串放入session中
        session.setAttribute("image_verify_code",randomStr);

        // 3 根据随机字符串生成图片&将图片以流的形式返回给前端
        ServletOutputStream os = response.getOutputStream();
        VerifyCodeUtils.outputImage(200,40,os,randomStr);
    }*/

    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult getRegisterImageCode(@PathVariable("imageCodeKey")String imageCodeKey) throws Exception{
        String base64Str = verifyCodeService.getRegisterImageCode(imageCodeKey);
        return AjaxResult.success(base64Str);
    }

    @PostMapping("/sms/register")
    public AjaxResult registerSendSmsCode(@Valid @RequestBody RegisterSendSmsCodeDTO dto){
        verifyCodeService.registerSendSmsCode(dto);
        return AjaxResult.success();
    }

    @PostMapping("/sms/bind")
    public AjaxResult bindSendSmsCode(@RequestBody Map<String,Object> map){
        verifyCodeService.bindSendSmsCode(map);
        return AjaxResult.success();
    }
}
